/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sisgp;
import dao.UltimoNumeroDAO;

import model.UltimoNumero;

/**
 *
 * @author Sandro
 */
public class AutoNumber {
    
    public AutoNumber()
    {
        
    }
    
    private SisMsg msg;
    private SisMsg msg1;
    
    public int getSequencial(String tabela)
    {
        
        UltimoNumeroDAO ultnumdao = new UltimoNumeroDAO();
        
        UltimoNumero ultnum = new UltimoNumero();
        
        int sequencial = 0;
        
        try{
            
            ultnum = ultnumdao.findUltNumero(tabela);
            sequencial = ultnum.getUltnumero();
        }
        catch (Exception ex) {
        
            sequencial = 0;
        
        }
        //msg.Info("go to hell", ultnum.toString());
        
        
        
        //sequencial = ultnum.getUltnumero();
        
        //msg1.Info("go to hell 2", ""+sequencial);
        
        int sequencialaux = sequencial;
        
        sequencial += 1;
        
        UltimoNumero ultimo = new UltimoNumero();
        
        
        ultimo.setTabela(tabela);
        
        ultimo.setUltnumero(sequencial);
        
        
        
        if (sequencialaux == 0)
        {
            
            //msg1.Info("go to hell 2", "entrou no if");
            ultnumdao.insert(ultimo);
        }
        else
        {
            
            //msg1.Info("go to hell 2", "entrou no else");
            
            ultnumdao.update(ultimo);
        }
        
        return sequencial;
        
    }        
            
    
}
